using System;
using System.Collections;
using UnityEngine;

public class TabletopHighlight : MonoBehaviour
{
	[Serializable]
	public class HighlightDefinition
	{
		public Vector2 positionOnTable;

		public float width;

		public float height;

		public bool shouldMove;

		public Vector2 speed;

		public FloatRange xMoveRange;

		public FloatRange yMoveRange;

		public bool useTableDimensions;

		public string text;

		public Vector2 realPositionOnTable
		{
			get
			{
				if (useTableDimensions)
				{
					return Vector2.Scale(positionOnTable, new Vector2(Table.halfwidth, Table.halfLength));
				}
				return positionOnTable;
			}
		}

		public float realWidth
		{
			get
			{
				if (useTableDimensions)
				{
					return width * Table.halfwidth;
				}
				return width;
			}
		}

		public float realHeight
		{
			get
			{
				if (useTableDimensions)
				{
					return height * Table.halfLength;
				}
				return height;
			}
		}

		public bool isPositionInside(Vector3 position)
		{
			Vector2 vector = new Vector2(position.x, position.z) - realPositionOnTable;
			return Mathf.Abs(vector.x) <= realWidth * 0.5f && Mathf.Abs(vector.y) <= realHeight * 0.5f;
		}

		public FloatRange GetOnTableXRange()
		{
			FloatRange result = default(FloatRange);
			result.min = 0f - Table.halfwidth + realWidth * 0.5f;
			result.max = Table.halfwidth - realWidth * 0.5f;
			return result;
		}

		public FloatRange GetOnTableYRange()
		{
			FloatRange result = default(FloatRange);
			result.min = realHeight * 0.5f;
			result.max = Table.halfLength - realHeight * 0.5f;
			return result;
		}
	}

	public UILabel label;

	public UISprite highlight;

	public Vector2 highlightPosition;

	public HighlightDefinition currentHighlightDefinition;

	private Vector2 moveOffset = Vector2.zero;

	private Vector2 moveDirection = Vector2.one;

	private bool shouldMove;

	public bool IsHighlightHit(Vector3 position)
	{
		if (currentHighlightDefinition == null)
		{
			return false;
		}
		float realWidth = currentHighlightDefinition.realWidth;
		float realHeight = currentHighlightDefinition.realHeight;
		Vector2 vector = new Vector2(position.x, position.z) - highlightPosition;
		return Mathf.Abs(vector.x) <= realWidth * 0.5f && Mathf.Abs(vector.y) <= realHeight * 0.5f;
	}

	public void ShowHighlight(HighlightDefinition highlightDefinition, Color colour)
	{
		StopAllCoroutines();
		if (string.IsNullOrEmpty(highlightDefinition.text))
		{
			label.cachedGameObject.SetActive(false);
		}
		else
		{
			label.text = highlightDefinition.text;
			label.cachedGameObject.SetActive(true);
		}
		currentHighlightDefinition = highlightDefinition;
		moveOffset = Vector2.zero;
		shouldMove = highlightDefinition.shouldMove;
		Vector3[] worldCorners = highlight.worldCorners;
		float num = Mathf.Abs(worldCorners[0].z - worldCorners[1].z);
		float num2 = Mathf.Abs(worldCorners[1].x - worldCorners[2].x);
		highlight.width = (int)((float)highlight.width * highlightDefinition.realWidth / num2);
		highlight.height = (int)((float)highlight.height * highlightDefinition.realHeight / num);
		SetHighlightPosition(highlightDefinition.realPositionOnTable);
		colour.a = 0.5f;
		highlight.color = colour;
		highlight.gameObject.SetActive(true);
	}

	public void ChangeHighlightColour(Color colour, bool blink = true)
	{
		colour.a = highlight.color.a;
		highlight.color = colour;
		highlight.cachedGameObject.GetComponent<TweenAlpha>().enabled = blink;
		if (!blink)
		{
			highlight.alpha = 0.5f;
		}
	}

	public void FlashColor(Color fromColor, Color toColor, float duration)
	{
		if (base.gameObject.activeInHierarchy)
		{
			highlight.cachedGameObject.GetComponent<TweenAlpha>().enabled = false;
			StopAllCoroutines();
			StartCoroutine(DoFlashColor(fromColor, toColor, duration));
		}
	}

	private IEnumerator DoFlashColor(Color fromColor, Color toColor, float duration)
	{
		fromColor.a = (toColor.a = 0.5f);
		highlight.color = toColor;
		yield return StartCoroutine(UIAnimTools.DoWaitRealTime(duration));
		highlight.color = fromColor;
	}

	public void SetHighlightHit(Color color)
	{
		StopAllCoroutines();
		ChangeHighlightColour(color, false);
		shouldMove = false;
	}

	public void HideHighlight()
	{
		highlight.gameObject.SetActive(false);
		label.text = string.Empty;
	}

	public void Update()
	{
		if (currentHighlightDefinition != null && shouldMove)
		{
			moveOffset += Vector2.Scale(currentHighlightDefinition.speed * Time.deltaTime, moveDirection);
			Vector2 realPositionOnTable = currentHighlightDefinition.realPositionOnTable;
			FloatRange xMoveRange = currentHighlightDefinition.xMoveRange;
			FloatRange yMoveRange = currentHighlightDefinition.yMoveRange;
			FloatRange onTableXRange = currentHighlightDefinition.GetOnTableXRange();
			FloatRange onTableYRange = currentHighlightDefinition.GetOnTableYRange();
			float num = Mathf.Sign(realPositionOnTable.y);
			xMoveRange.min = onTableXRange.Clamp(currentHighlightDefinition.xMoveRange.min);
			xMoveRange.max = onTableXRange.Clamp(xMoveRange.max);
			yMoveRange.min = onTableYRange.Clamp(Mathf.Abs(yMoveRange.min) * Table.halfLength);
			yMoveRange.max = onTableYRange.Clamp(Mathf.Abs(yMoveRange.max) * Table.halfLength);
			Vector2 vector = realPositionOnTable + moveOffset;
			vector.x = xMoveRange.Clamp(vector.x);
			vector.y = yMoveRange.Clamp(vector.y);
			if (xMoveRange.InverseLerp(vector.x) == 1f && moveDirection.x > 0f)
			{
				moveDirection.x = -1f;
			}
			else if (xMoveRange.InverseLerp(vector.x) == 0f && moveDirection.x < 0f)
			{
				moveDirection.x = 1f;
			}
			if (yMoveRange.InverseLerp(Mathf.Abs(vector.y)) == 1f && moveDirection.y == num)
			{
				moveDirection.y = 0f - num;
			}
			else if (yMoveRange.InverseLerp(Mathf.Abs(vector.y)) == 0f && moveDirection.y != num)
			{
				moveDirection.y = num;
			}
			SetHighlightPosition(vector);
		}
	}

	public void SetHighlightPosition(Vector2 position)
	{
		highlightPosition = position;
		highlight.transform.localPosition = new Vector3(position.x / base.transform.localScale.x, position.y / base.transform.localScale.y, 0f);
		label.cachedTransform.localPosition = highlight.transform.localPosition + Vector3.forward * -0.1f / base.transform.localScale.y;
		Debug.Log("Local pos " + label.cachedTransform.localPosition);
	}
}
